package zhxt.helmet.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.SessionAttribute;
import zhxt.helmet.dao.PopedomDao;
import zhxt.helmet.pojo.*;

import java.util.ArrayList;
import java.util.List;

@Service("popedomService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)

public class PopedomService {
    @Autowired
    PopedomDao popedomDao;

    //获取全部省份列表
    public List<Province> getProvinces( User user){
        List<Province> provinceList;
        if(user.getProvinceId() != 2) {//用户辖区是一个特定的省 或 无
            provinceList = new ArrayList<Province>();
            provinceList.add(new Province(user.getProvinceId(), user.getProvinceName()));
        }
        else  {//全部
            provinceList = popedomDao.selectAllProvinces();
        }
        return provinceList;
    }
    //根据省id获取省内全部市
    public List<City> getCitiesByProvinceId(int provinceId, User user) {
        List<City> cityList= new ArrayList<>();;
        if(user.getCityId() != 2) {//用户辖区是一个特定的市 或 无
            cityList.add(new City(user.getCityId(),user.getProvinceId(),user.getCityName()));
        }
        else  {//用户辖区市是全部
            cityList.add(new City(2,2,"全部"));
            cityList.addAll(popedomDao.selectCitiesByProvinceId(provinceId));
        }
        return cityList;
    }

    //根据市id获取市内全部区
    public List<District> getDistrictsByCityId(int cityId, User user) {
        List<District> districtList= new ArrayList<>();;
        if(user.getDistrictId() != 2) {
            districtList.add(new District(user.getDistrictId(),user.getCityId(),user.getDistrictName()));
        }
        else {
            districtList.add(new District(2,2,"全部"));
            districtList.addAll(popedomDao.selectDistrictsByCityId(cityId));
        }
        return districtList;
    }
    //根据区id获取区内全部块
    public List<Block> getBlocksByDistrictId(int districtId, User user) {
        List<Block> blockList= new ArrayList<>();;
        if(user.getBlockId() != 2) {
            blockList.add(new Block(user.getBlockId(),user.getDistrictId(),user.getBlockName()));
        }
        else {
            blockList.add(new Block(2,2,"全部"));
            blockList.addAll(popedomDao.selectBlocksByDistrictId(districtId));
        }
        return blockList;
    }
}
